package com.mtons.member.account.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mtons.core.entity.po.AccountPO;
import com.mtons.core.interfaces.dao.IAccountDao;
import com.mtons.core.interfaces.service.IAccountService;

@Service("accountService")
@Transactional
public class AccountService implements IAccountService {

	@Autowired
	private IAccountDao accountDao;
	
	@Override
	public AccountPO findById(int id) {
		return this.accountDao.findById(id);
	}

	@Override
	public void saveAccount(AccountPO po) {
		this.accountDao.saveAccount(po);
	}

	@Override
	public void updateAccount(AccountPO po) {
		AccountPO entity = this.accountDao.findById(po.getId());
		if(entity != null){
			entity.setBirth(po.getBirth());
			entity.setPassword(po.getPassword());
			entity.setSalary(po.getSalary());
			entity.setSsn(po.getSsn());
			entity.setUsername(po.getUsername());
		}
	}

	@Override
	public void deleteAccount(String ssn) {
		this.accountDao.deleteAccount(ssn);
	}

	@Override
	public List<AccountPO> findAllAccounts() {
		return this.accountDao.findAllAccount();
	}

	@Override
	public AccountPO findAccountBySsn(String ssn) {
		return this.accountDao.findAccountBySsn(ssn);
	}

	@Override
	public boolean isAccountSsnUnique(Integer id, String ssn) {
		AccountPO entity = this.accountDao.findById(id);
		return ( entity == null || ((id != null) && (entity.getId() == id)));
	}

}
